package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.MangerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/product")
public class SpuManageController {

    @Autowired
    MangerService mangerService;
    ///admin/product/{page}/{limit}
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo){
        Page pageModel = new Page(page,limit);
        IPage<SpuInfo> ipage = this.mangerService.getSpuInfoList(pageModel,spuInfo);
        return Result.ok(ipage);
    }
    // /admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){

        List<BaseSaleAttr> baseSaleAttrList = mangerService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }
    // /admin/product/saveSpuInfo
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        mangerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //  getSpuInfo 回显
    @GetMapping("getSpuInfo/{spuId}")
    public Result getSpuInfo(@PathVariable Long spuId){
        SpuInfo spuInfoList = mangerService.getSpuInfo(spuId);
        return Result.ok(spuInfoList);
    }
    //updateSpuInfo(SpuInfo spuInfo)
    @PostMapping("updateSpuInfo")
    public Result updateSpuInfo(@RequestBody SpuInfo spuInfo){
        mangerService.updateSpuInfo(spuInfo);
        return Result.ok();
    }
    // /admin/product/spuImageList/{spuId}
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){
       List<SpuImage> spuImageList = this.mangerService.spuImageList(spuId);
       return Result.ok(spuImageList);
    }
    // /admin/product/spuSaleAttrList/{spuId}
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = this.mangerService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
}
